import type { AxiosRequestConfig } from 'axios';
import type { RequestInstanceState } from './type';
import { Notify } from 'quasar';

/**
 * refresh token
 *
 * @param axiosConfig - request config when the token is expired
 */
export async function handleRefreshToken(axiosConfig: AxiosRequestConfig) {
  console.log('handleRefreshToken');
  console.log('axiosConfig :>> ', axiosConfig);
  return null;
}

export function showErrorMsg(state: RequestInstanceState, message: string) {
  if (!state.errMsgStack?.length) {
    state.errMsgStack = [];
  }

  const isExist = state.errMsgStack.includes(message);

  if (!isExist) {
    state.errMsgStack.push(message);

    Notify.create({
      type: 'negative',
      message: message,
      position: 'top',
      progress: true,
      actions: [{ icon: 'close', color: 'white' }],
      onDismiss() {
        state.errMsgStack = state.errMsgStack.filter((msg) => msg !== message);
        setTimeout(() => {
          state.errMsgStack = [];
        }, 5000);
      },
    });
  }
}
